package com.willian.andrappfw.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.willian.andrappfw.R.anim;

public class UIUtils {
    public UIUtils() {
    }

    public static int getStatusBarHeight(Context context) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return resourceId > 0 ? context.getResources().getDimensionPixelSize(resourceId) : 0;
    }

    public static int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        return resourceId > 0 ? resources.getDimensionPixelSize(resourceId) : 0;
    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue * scale + 0.5F);
    }

    public static void showUnderline(TextView textView) {
        String text = textView.getText().toString();
        if (text.length() != 0) {
            SpannableString ss = new SpannableString(text);
            ss.setSpan(new UnderlineSpan(), 0, text.length(), 17);
            textView.setText(ss);
        }
    }

    public static void fadeIn(final View view) {
        if (view != null && view.getVisibility() != 0) {
            ValueAnimator animator = ValueAnimator.ofFloat(new float[]{0.0F, 1.0F});
            animator.addUpdateListener((animation) -> {
                view.setAlpha((Float)animation.getAnimatedValue());
            });
            animator.addListener(new AnimatorListenerAdapter() {
                public void onAnimationStart(Animator animation) {
                    view.setVisibility(0);
                }
            });
            animator.setDuration(300L);
            animator.start();
        }
    }

    public static void fadeOut(final View view) {
        if (view != null && view.getVisibility() == 0) {
            ValueAnimator animator = ValueAnimator.ofFloat(new float[]{1.0F, 0.0F});
            animator.addUpdateListener((animation) -> {
                view.setAlpha((Float)animation.getAnimatedValue());
            });
            animator.addListener(new AnimatorListenerAdapter() {
                public void onAnimationEnd(Animator animation) {
                    view.setVisibility(8);
                }
            });
            animator.setDuration(300L);
            animator.start();
        }
    }

    public static void bottomIn(final View view) {
        if (view != null && view.getVisibility() != 0) {
            Animation animation = AnimationUtils.loadAnimation(view.getContext(), anim.bottom_in);
            animation.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                    view.setVisibility(0);
                }

                public void onAnimationEnd(Animation animation) {
                }

                public void onAnimationRepeat(Animation animation) {
                }
            });
            view.startAnimation(animation);
        }
    }

    public static void bottomOut(final View view) {
        if (view != null && view.getVisibility() == 0) {
            Animation animation = AnimationUtils.loadAnimation(view.getContext(), anim.bottom_out);
            animation.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                }

                public void onAnimationEnd(Animation animation) {
                    view.setVisibility(8);
                }

                public void onAnimationRepeat(Animation animation) {
                }
            });
            view.startAnimation(animation);
        }
    }

    public static void addStatusbarHeightPadding(View view) {
        if (view != null) {
            view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(view.getContext()), view.getPaddingRight(), view.getPaddingBottom());
        }
    }

    public static void addStatusbarHeightMargin(View view) {
        if (view != null && view.getLayoutParams() != null && view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams var10000 = (ViewGroup.MarginLayoutParams)view.getLayoutParams();
            var10000.topMargin += getStatusBarHeight(view.getContext());
            view.requestLayout();
        }
    }

    public static void hideInputMethod(View view) {
        InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService("input_method");
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static <T extends View> T getChildView(View parent, int childId, Class<T> clazz) {
        return parent.findViewById(childId);
    }
}
